package com_.dxy_test_.zookeeper_.common_config_;

import com.alibaba.fastjson.JSON;
import com_.dxy_test_.zookeeper_.ZkConfig;
import org.I0Itec.zkclient.ZkClient;

public class ZkConfigMag {

    private Config config;

    /**
     * 从数据库加载配置
     */
    public Config downLoadConfigFromDB() {
        //getDB
        config = new Config("nm", "pw");
        return config;
    }

    /**
     * 配置文件上传到数据库
     */
    public void upLoadConfigToDB(String nm, String pw) {
        if (config == null) config = new Config();
        config.setUserNm(nm);
        config.setUserPw(pw);
        //updateDB
    }

    /**
     * 配置文件同步到zookeeper
     */
    public void syncConfigToZk() {
        ZkClient zk = ZkConfig.zkClient;
        if (!zk.exists(ZkConfig.CONFIG_PATH)) {
            zk.createPersistent(ZkConfig.CONFIG_PATH, true);
        }
        zk.writeData(ZkConfig.CONFIG_PATH, JSON.toJSONString(config));
    }

    /**
     * 删除配置
     */
    public void deleteConfig() {
        ZkClient zk = ZkConfig.zkClient;
        if (!zk.exists(ZkConfig.CONFIG_PATH)) {
            return;
        }
        zk.delete(ZkConfig.CONFIG_PATH);
    }
}
